<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id 
 * @property int $course_id 
 * @property int $parent_id 
 * @property string $name 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $creator_id 
 * @property-read null|self $parent 
 */
class CourseSection extends Model
{
    use Traits\HasCreator;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'course_section';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'course_id', 'parent_id', 'name', 'created_at', 'updated_at', 'creator_id'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'course_id' => 'integer', 'parent_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'creator_id' => 'integer'];


    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id')->withDefault([
            'name' => '无',
            'id' => 0
        ]);
    }
    public function children()
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    public function tasks(){
        return $this->hasMany(CourseTask::class,'section_id');
    }
}
